package org.freedoit.server.dao;

// Generated Oct 11, 2012 3:54:06 PM by Hibernate Tools 3.4.0.CR1

import java.sql.Timestamp;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import org.freedoit.server.om.Project;
import org.freedoit.shared.exception.ServerException;

/**
 * Home object for domain model class Project.
 * @see org.freedoit.om.Project
 * @author Hibernate Tools
 */
public class ProjectDAO {

	//private static final Log log = LogFactory.getLog(ProjectDAO.class);

	private final EntityManagerFactory emFactory = DAOFactory.getEntityManagerFactory();

	public void persist(Project project) {
		//log.debug("persisting Project instance");
		EntityManager em = emFactory.createEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(project);

			em.getTransaction().commit();
			//log.debug("persist successful");
		} catch (RuntimeException re) {
			//log.error("persist failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public void update(Project project) {
		//log.debug("updating Project instance");
		EntityManager em = emFactory.createEntityManager();
		try {
			em.getTransaction().begin();
			em.merge(project);
			em.getTransaction().commit();
			//log.debug("update successful");
		} catch (RuntimeException re) {
			//log.error("update failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public Project findById(Long id) {
		//log.debug("getting Project instance with id: " + id);
		EntityManager em = emFactory.createEntityManager();
		try {
			Project instance = em.find(Project.class, id);
			if (instance == null) {
				//log.debug("get successful, no instance found");
			} else {
				//log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			//log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public List<Project> findByUserId(Long userId) {
		//log.debug("finding Project instances by user id:" + userId);
		EntityManager em = emFactory.createEntityManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("from ").append(Project.class.getName());
			sbQuery.append(" where userId = ").append(userId);
			List<Project> results = em.createQuery(sbQuery.toString(), Project.class).getResultList();
			
//			int iSize = 0;
//			if (results != null) {
//				iSize = results.size();
//			}
			//log.debug("find by user id successful, result size: " + iSize);
			return results;
		} catch (RuntimeException re) {
			//log.error("find by user id failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public List<Project> findLatest(Long userId, Long clientId, Timestamp lastModifyTime, 
			Timestamp now) {
		//log.debug("finding Project instances by user id:" + userId);
		EntityManager em = emFactory.createEntityManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("from ").append(Project.class.getName());
			sbQuery.append(" where userId = ").append(userId);
			sbQuery.append(" and lastModifyByClientId <> ").append(clientId);
			
			if (lastModifyTime != null) {
				sbQuery.append(" and lastModifyTime between ?1 and ?2 ");
			} else {
				sbQuery.append(" and lastModifyTime < ?1 ");
			}

			TypedQuery<Project> q = em.createQuery(sbQuery.toString(), Project.class);
			
			if (lastModifyTime != null) {
				q.setParameter(1, lastModifyTime, TemporalType.TIMESTAMP);
				q.setParameter(2, now, TemporalType.TIMESTAMP);
			} else {
				q.setParameter(1, now, TemporalType.TIMESTAMP);
			}
			List<Project> results = q.getResultList();
			
//			int iSize = 0;
//			if (results != null) {
//				iSize = results.size();
//			}
			//log.debug("find by user id successful, result size: " + iSize);
			return results;
		} catch (RuntimeException re) {
			//log.error("find by user id failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}
}
